﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAO
{
    public class DonViTinhDAO
    {
        public List<DonViTinhDTO> LayDanhSachDonViTinh()
        {
            List<DonViTinhDTO> danhsach = new List<DonViTinhDTO>();
            string sql = "Select * From DONVITINH";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                DonViTinhDTO dvDTO = new DonViTinhDTO();
                dvDTO.MaDonViTinh = reader.GetInt32(0);
                dvDTO.TenDonViTinh = reader.GetString(1);
                danhsach.Add(dvDTO);
            }
            conn.Close();
            return danhsach;
        }
        public string LayTenDonViTinh(int maDV)
        {
            string ten = "";
            string sql = "Select TenDonViTinh From DONVITINH Where MaDonViTinh= " + maDV;

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ten = reader.GetString(0);
            }
            conn.Close();
            return ten;
        }
        public int LayMaDonViTinh(string ten)
        {
            int ma = 0;
            string sql = "Select MaDonViTinh From DONVITINH Where TenDonViTinh= " + "N'" + ten + "'";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ma = reader.GetInt32(0);
            }
            conn.Close();
            return ma;
        }
    }
}
